import {
  calendar
} from 'calendar-model.js';
import {
  Config
} from '../../../utils/config.js';
var util = require('../../../utils/util.js');
var Function = require("../../../utils/function.js");
var api = new calendar(); //实例化 首页 对象
var getUserInfo = require('../../auth/auth.js')
var app = getApp();
Page({
  data: {

    //这是刚新增部分（onLoad中有新增）
    he: 100,
    odht: 100,
    array: [{}, {}, {}, {}, {}],
    sind: 1,
    //新增部分 结束


    year: '',
    month: '',
    day: '',
    weekArr: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    firstDay: '',
    lastDay: '',
    //当前时间
    currentMonths: '',
    currentYears: '',
    height:0,

    param: null,
    clockNum: 3,

    //元素宽度
    SonWidth: false,
  },
  punch: function(event) {

    var day = event.currentTarget.dataset.day;
    console.log(day)
    this.setData({
      day: day
    })
    this.bindSign();
  },
  //打卡信息
  bindSign() {
    if (this.data.year){
      var date = this.data.year + '-' + this.data.month + '-' + this.data.day;
    }else{
      var date='';
    }
    var odht=this.data.odht;
    api.getSign({ date: date,is_log:1 }, (res) => {
      if(res.code==0){
        app.showError(res.msg, function () {
          wx.navigateBack();
        });
      }else{
        var height = res.data.punch.length * odht - odht;
        var he = res.data.punch.length * odht;
        this.setData({
          details: res.data,
          height: height,
          he: he
        })
      }
    })
  },

  getDate: function() { //获取当月日期
    var mydate = new Date();
    var year = mydate.getFullYear();
    var month = mydate.getMonth();
    var months = month + 1;
    this.data.year = year;
    this.data.month = months;
    this.data.day = mydate.getDate();
    var fist = new Date(year, month, 1);
    this.data.firstDay = fist.getDay();
    var last = new Date(year, months, 0);
    this.data.lastDay = last.getDate();

    this.setData({
      currentYears: year,
      currentMonths: months,
      year: this.data.year,
      month: this.data.month,
      day: this.data.day,
      firstDay: this.data.firstDay,
      lastDay: this.data.lastDay
    })
  },
  setDate: function() {
    for (var i = 1; i < this.data.lastDay + 1; i++) {
      this.data.dateArr.push(i);
    }
    this.setData({
      dateArr: this.data.dateArr,
      firstDay: this.data.firstDay
    })
  },
  prevMonth: function() { //上一月
    var months = "";
    var years = "";
    if (this.data.month == 1) {
      years = this.data.year - 1
      this.data.month = 12;
      months = this.data.month;
    } else {
      years = this.data.year;
      months = this.data.month - 1;
    }

    var first = new Date(years, months - 1, 1);
    this.data.firstDay = first.getDay();
    var last = new Date(years, months, 0);
    this.data.lastDay = last.getDate();

    this.setData({
      month: months,
      year: years,
      firstDay: this.data.firstDay,
      lastDay: this.data.lastDay
    })

    this.data.dateArr = [];
    for (var i = 1; i < this.data.lastDay + 1; i++) {
      this.data.dateArr.push(i);
    }
    this.setData({
      dateArr: this.data.dateArr
    })
  },
  nextMonth: function() { //下一月
    var months = "";
    var years = "";
    if (this.data.month == 12) {
      this.data.month = 0;
      months = this.data.month;
      years = this.data.year + 1;
    } else {
      months = this.data.month + 1;
      years = this.data.year;
    }
    var months = this.data.month + 1;
    var first = new Date(years, months - 1, 1);
    this.data.firstDay = first.getDay();
    var last = new Date(years, months, 0);
    this.data.lastDay = last.getDate();
    this.setData({
      month: months,
      year: years,
      firstDay: this.data.firstDay,
      lastDay: this.data.lastDay
    })

    this.data.dateArr = [];
    for (var i = 1; i < this.data.lastDay + 1; i++) {
      this.data.dateArr.push(i);
    }
    this.setData({
      dateArr: this.data.dateArr
    })
  },
  onLoad: function(options) {
    let _this = this;
    _this.setData({
      isLogin: app.checkIsLogin()
    });
    if (_this.data.isLogin === false) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    this.bindSign();
 

    this.getDate();
    this.setDate();
    // wx.createSelectorQuery().select('.date-item').boundingClientRect(res => {
    //   this.setData({
    //     SonWidth: res.width
    //   })
    // }).exec();

  },

})